package org.hishenframework.core.web.message;

import org.springframework.validation.BindingResult;

public class Message {

	public final static String SAVE_METHOD = "save";
	public final static String UPDATE_METHOD = "update";
	public final static String DELETE_METHOD = "delete";
	
	private final static String SAVE_SUCCESS = "保存成功!";
	private final static String UPDATE_SUCCESS = "修改成功!";
	private final static String DELETE_SUCCESS = "删除成功!";
	private final static String DEFUALT_SUCCESS = "操作成功!";
	
	private final static String SAVE_FAIL = "保存失败!";
	private final static String UPDATE_FAIL = "修改失败!";
	private final static String DELETE_FAIL = "删除失败!";
	private final static String DEFUALT_FAIL = "操作失败!";
	
	private final static String VALIDATE_NOTPASSED = "验证未通过!"; 
	
	private String text;
	
	/** 设置类型 */
	private String methodName;
	
	private BindingResult result;
	
	private long resultCount = -1;
	
	private String detail = "";

	public void build() {
		StringBuilder textBuilder = new StringBuilder();
		if (result != null && result.hasErrors() || resultCount == 0) {
			switch (methodName) {
			case "save":
				textBuilder.append(Message.SAVE_FAIL);
				break;
			case "update":
				textBuilder.append(Message.UPDATE_FAIL);
				break;
			case "delete":
				textBuilder.append(Message.DELETE_FAIL);
				break;
			default:
				textBuilder.append(Message.DEFUALT_FAIL);
				break;
			}
		} else {
			switch (methodName) {
			case "save":
				textBuilder.append(Message.SAVE_SUCCESS);
				break;
			case "update":
				textBuilder.append(Message.UPDATE_SUCCESS);
				break;
			case "delete":
				textBuilder.append(Message.DELETE_SUCCESS);
				break;
			default:
				textBuilder.append(Message.DEFUALT_SUCCESS);
				break;
			}
		}
		
		if (result != null && result.hasErrors()) {
			textBuilder.append(" " + VALIDATE_NOTPASSED);
		}
		textBuilder.append(" " + detail);
		this.text = textBuilder.toString();
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
	
	/**
	 * 设置结果
	 * @param result
	 */
	public void setResult(BindingResult result) {
		this.result = result;
	}

	/**
	 * 设置方法名称
	 * @param result
	 */
	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}
	
	public String getMethodName() {
		return methodName;
	}

	/**
	 * 设置结果数量
	 * @param result
	 */
	public void setResultCount(long resultCount) {
		this.resultCount = resultCount;
	}
	
	/**
	 * 设置详细
	 * @param detail
	 */
	public void setDetail(String detail) {
		this.detail = detail;
	}
}
